package com.shiminghui.test6;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Student[] students = new Student[3];

        Student student1 = new Student("1", "tom", 18);
        Student student2 = new Student("2", "tom", 18);
        Student student3 = new Student("3", "tom", 18);

        students[0]=student1;
        students[1]=student2;
        students[2]=student3;

        Student student4 = new Student("3","jike",25);

        if (!contains(students,student4)){
            if (getLength(students)<students.length){
                students[getLength(students)]=student4;
                print(students);
            }else {
                Student[] students1 = copy(students);
                students1[students1.length-1]=student4;
                print(students1);
            }

        }else {
            System.out.println("id重复,重新输入");

        }

        int index=getIndex(students,"2");
        if (index>=0){
            System.out.println("删除成功");
            students[index]=null;
            print(students);
        }else {
            System.out.println("删除失败");
        }

        int index1=getIndex(students,"3");
        if (index>=0){
            System.out.println("查询成功");
            students[index1].setAge(students[index1].getAge()+1);
            print(students);
        }else {
            System.out.println("查询失败");
        }


    }

    public static boolean contains(Student[] students,Student student){
        for (int i = 0; i < students.length; i++) {
            if (students[i].getSno().equals(student.getSno())){
                return true;
            }
        }
        return false;
    }

    public static int getLength(Student[]  students){
        int count=0;
        for (int i = 0; i < students.length; i++) {
            if (students[i]!=null){
                count++;
            }
        }
        return count;
    }

    public static Student[] copy(Student[] students){
        Student[] students1 = new Student[students.length+1];
        for (int i = 0; i < students.length; i++) {
            students1[i]=students[i];
        }
        return students1;
    }
    public static void print(Student[] students){
        for (int i = 0; i < students.length; i++) {
            if (students[i]!=null){
                System.out.println(students[i].getSno()+" "+students[i].getName()+" "+students[i].getAge());
            }
        }
    }
    public static int getIndex(Student[] students,String sno){
        for (int i = 0; i < students.length; i++) {
            if (students[i]!=null){
                if (students[i].getSno().equals(sno)){
                    return i;
                }
            }
        }
        return -1;
    }
}
